# encoding:utf-8
import json
import re
import requests
from bs4 import BeautifulSoup


from Hero import Hero
from Talent import Talent
from Skill import Skill


# # region heroes

# heroes = []
# transfers = []
# heroSoup = BeautifulSoup(
#     requests.get("https://wiki.biligame.com/tdj/index.php?title=分类:角色").text,
#     "html.parser",
# )
# all_heroes = heroSoup.select("#mw-pages > div > div")[0].select("a")
# for i in all_heroes:
#     heroName = i.text
#     heroInfoSoup = BeautifulSoup(
#         requests.get(
#             f"https://wiki.biligame.com/tdj/index.php?title={heroName}&action=edit"
#         ).text,
#         "html.parser",
#     )
#     heroInfo = heroInfoSoup.select("#wpTextbox1")[0].text
#     hero = Hero(heroInfo)
#     heroes.append(hero)
#     print(f"英雄: {heroName}")

# with open("heroes.json", "w", encoding="utf-8") as f:
#     f.write(
#         json.dumps(
#             heroes,
#             ensure_ascii=False,
#             indent=2,
#             sort_keys=False,
#             separators=(",", ": "),
#         )
#     )

# # endregion


# # region talents

# talents = []
# talentSoup = BeautifulSoup(
#     requests.get(
#         "https://wiki.biligame.com/tdj/index.php?title=分类:天赋&action=edit&redlink=1"
#     ).text,
#     "html.parser",
# )
# all_talents = talentSoup.select("#mw-pages > div > div")[0].select("a")
# for i in all_talents[2:]:
#     talentName = i.text
#     talentInfoSoup = BeautifulSoup(
#         requests.get(
#             f"https://wiki.biligame.com/tdj/index.php?title={talentName}&action=edit"
#         ).text,
#         "html.parser",
#     )
#     talentInfo = talentInfoSoup.select("#wpTextbox1")[0].text
#     talents.append(Talent(talentInfo))
#     print(f"天赋: {talentName}")

# with open("talents.json", "w", encoding="utf-8") as f:
#     f.write(
#         json.dumps(
#             talents,
#             ensure_ascii=False,
#             indent=2,
#             sort_keys=False,
#             separators=(",", ": "),
#         )
#     )

# # endregion

# region skills

skills = []
skillSoup = BeautifulSoup(
    requests.get("https://wiki.biligame.com/tdj/绝学列表").text, "html.parser"
)
all_skills = skillSoup.select("#CardSelectTr > tbody > tr")[1:]
for i in all_skills:
    skillName = i.select("td")[1].select("a")[0].text
    if skillName != "天机尺":
        skillInfoSoup = BeautifulSoup(
            requests.get(
                f"https://wiki.biligame.com/tdj/index.php?title=绝学/{skillName}&action=edit"
            ).text,
            "html.parser",
        )
        skillInfo = skillInfoSoup.select("#wpTextbox1")[0].text
        skills.append(Skill(skillInfo))
    print(f"绝学：{skillName}")

with open("skills.json", "w", encoding="utf-8") as f:
    f.write(
        json.dumps(
            skills,
            ensure_ascii=False,
            indent=2,
            sort_keys=False,
            separators=(",", ": "),
        )
    )

# endregion

print("OK")
